package com.icongyou.enterprise.talent_analysis.controller;

import com.icongyou.enterprise.common.result.Result;
import com.icongyou.enterprise.talent_analysis.entity.JobRecommendation;
import com.icongyou.enterprise.talent_analysis.entity.StudentInsight;
import com.icongyou.enterprise.talent_analysis.entity.TaskCategoryAnalysis;
import com.icongyou.enterprise.talent_analysis.service.AIInsightCacheService;
import com.icongyou.enterprise.talent_analysis.service.AIInsightService;
import com.icongyou.enterprise.talent_analysis.service.HrTemplatePreferenceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * AI辅助洞察控制器
 * 提供学生岗位推荐、优缺点分析等功能的REST API
 * 支持缓存优先策略+异步重新生成
 */
@Slf4j
@RestController
@RequestMapping("/api/talent-analysis/ai-insight")
@RequiredArgsConstructor
public class AIInsightController {
    
    private final AIInsightService aiInsightService;
    private final AIInsightCacheService cacheService;
    private final HrTemplatePreferenceService preferenceService;
    
    /**
     * 生成学生完整AI洞察报告
     * 包含岗位推荐、优缺点分析、综合评价等
     * 支持默认6维度和自定义维度模板
     * 
     * 策略: 优先从缓存读取,缓存未命中则实时生成并异步更新缓存
     */
    @GetMapping("/student/{studentId}")
    public Result<StudentInsight> getStudentInsight(
            @PathVariable String studentId,
            @RequestParam(value = "hrUserId", required = false) String hrUserId) {
        try {
            
            // 获取模板ID
            String templateId = null;
            if (hrUserId != null) {
                templateId = preferenceService.getCurrentTemplateId(hrUserId);
            }
            
            // 1. 尝试从缓存获取
            StudentInsight cachedInsight = cacheService.getFromCache(studentId, templateId);
            if (cachedInsight != null) {
                return Result.success(cachedInsight);
            }
            
            // 2. 缓存未命中,实时生成
            StudentInsight insight = aiInsightService.generateStudentInsight(studentId, hrUserId);
            
            // 3. 异步更新缓存
            cacheService.putToCache(studentId, templateId, insight);
            
            return Result.success(insight);
        } catch (Exception e) {
            return Result.error("生成AI洞察失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取岗位推荐
     * 支持默认6维度和自定义维度模板
     */
    @GetMapping("/student/{studentId}/jobs")
    public Result<List<JobRecommendation>> getJobRecommendations(
        @PathVariable String studentId,
        @RequestParam(defaultValue = "3") int topN,
        @RequestParam(value = "hrUserId", required = false) String hrUserId
    ) {
        try {
            
            if (topN < 1 || topN > 5) {
                return Result.error("推荐数量必须在1-5之间");
            }
            
            List<JobRecommendation> recommendations = aiInsightService.recommendJobs(studentId, topN, hrUserId);
            return Result.success(recommendations);
        } catch (Exception e) {
            return Result.error("生成岗位推荐失败：" + e.getMessage());
        }
    }
    
    /**
     * 分析学生优缺点
     * 支持默认6维度和自定义维度模板
     */
    @GetMapping("/student/{studentId}/strengths-weaknesses")
    public Result<Map<String, List<String>>> analyzeStrengthsWeaknesses(
            @PathVariable String studentId,
            @RequestParam(value = "hrUserId", required = false) String hrUserId) {
        try {
            Map<String, List<String>> result = aiInsightService.analyzeStrengthsAndWeaknesses(studentId, hrUserId);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("分析优缺点失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取任务类别表现分析
     */
    @GetMapping("/student/{studentId}/task-performance")
    public Result<TaskCategoryAnalysis> getTaskCategoryAnalysis(@PathVariable String studentId) {
        try {
            TaskCategoryAnalysis analysis = aiInsightService.analyzeTaskCategoryPerformance(studentId);
            return Result.success(analysis);
        } catch (Exception e) {
            return Result.error("分析任务表现失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取职业发展建议
     * 支持默认6维度和自定义维度模板
     */
    @GetMapping("/student/{studentId}/career-advice")
    public Result<String> getCareerAdvice(
            @PathVariable String studentId,
            @RequestParam(value = "hrUserId", required = false) String hrUserId) {
        try {
            String advice = aiInsightService.generateCareerAdvice(studentId, hrUserId);
            return Result.success(advice);
        } catch (Exception e) {
            return Result.error("生成职业建议失败：" + e.getMessage());
        }
    }
}
